package com.drojian.demo1.test2;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.drojian.demo.R;

public class SeekBarActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private  SeekBar seekBar1;
    private AudioManager audioManager;
    private TextView seekBarText2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.ui2_activity_seek_bar);

        Button back = findViewById(R.id.back);
        back.setOnClickListener(v->finish());

        seekBar1 = findViewById(R.id.seekBar1);
        seekBarText2 = findViewById(R.id.seekBarText2);


        audioManager= (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int max =audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int progress = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        seekBar1.setMax(max);
        seekBar1.setProgress(progress);
        //设置监听
        seekBar1.setOnSeekBarChangeListener(this);

    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int progress1 = seekBar1.getProgress();
        if(progress1 < 1)
            progress1 = 1;
        //对音乐生效  当前音量  0
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress1,0);
        seekBarText2.setText("当前音量为"+progress1);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}